// wenshidu/add_chufang/add_chufang.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
var shuzong = []
var shu = []
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    title: '修改许可证',
    state: '', //0添加 1修改
    heights: '',
    list_gao: '',
    xukexq: '',
    shopName: '',
    shopPerson: '',
    qiyePerson: '',
    zlPerson: '',
    magmentFanwei: '',
    magmentLeixing: '', //经营方式
    licenceCode: '',
    licenceImg: '',
    starTime: '',
    starTimes: '',
    endTime: '',
    endTimes: '',
    buttonClicked: false,
    xukekaishi:2020,
  },
  // 验证
  xubaocun() {
    if (this.data.shopName == '') {
      wx.showToast({
        title: '请输入许可证名称',
        icon: 'none'
      })
    } else if (this.data.shopPerson == '') {
      wx.showToast({
        title: '请输入法人/负责人',
        icon: 'none'
      })
    } else if (this.data.qiyePerson == '') {
      wx.showToast({
        title: '请输入企业负责人',
        icon: 'none'
      })
    } else if (this.data.zlPerson == '') {
      wx.showToast({
        title: '请输入质量负责人',
        icon: 'none'
      })
    } else if (this.data.magmentFanwei == '') {
      wx.showToast({
        title: '请输入经营范围',
        icon: 'none'
      })
    } else if (this.data.magmentLeixing == '') {
      wx.showToast({
        title: '请输入经营方式',
        icon: 'none'
      })
    } else if (this.data.licenceCode == '') {
      wx.showToast({
        title: '请输入许可证编号',
        icon: 'none'
      })
    } else if (this.data.starTime == '' || this.data.starTime == '请选择发证日期') {
      wx.showToast({
        title: '请选择发证日期',
        icon: 'none'
      })
    } else if (this.data.endTime == '' || this.data.endTime == '请选择有效期') {
      wx.showToast({
        title: '请选择有效期',
        icon: 'none'
      })
    } else {
      this.jiekou()
    }
  },
  jiekou() {
    this.myRequest(yuming + 'shop/shop_update_licence', {
      shopId: wx.getStorageSync('shopId'),
      id: this.data.xu_id,
      shopName: this.data.shopName,
      shopPerson: this.data.shopPerson,
      qiyePerson: this.data.qiyePerson,
      zlPerson: this.data.zlPerson,
      magmentFanwei: this.data.magmentFanwei,
      licenceCode:this.data.licenceCode,
      magmentLeixing: this.data.magmentLeixing, //经营方式
      licenceImg: this.data.wulicenceImg,
      starTime: this.data.starTimes,
      endTime: this.data.endTimes,
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)
      }
    })
  },
  input(e) {
    let val = e.detail.value
    let modal = e.currentTarget.dataset.modal
    console.log(val)
    console.log(modal)
    this.setData({
      [modal]: val
    })
  },
  xuke_xq() {
    this.myRequest(yuming + 'shop/shop_info_licence', {
      shopId: wx.getStorageSync('shopId'),
      id: this.data.xu_id,
    }, 'POST').then(res => {
      console.log(res)
      var data = res.data.data
      this.setData({
        xukexq: data,
        shopName: data.shopName,
        shopPerson: data.shopPerson,
        qiyePerson: data.qiyePerson,
        zlPerson: data.zlPerson,
        magmentFanwei: data.magmentFanwei,
        magmentLeixing: data.magmentLeixing, //经营方式
        licenceImg: yuming + data.licenceImg,
        wulicenceImg: data.licenceImg,
        starTime: data.starTime,
        starTimes: data.starTime,
        endTime: data.endTime,
        endTimes: data.endTime,
      })
      if (data.licenceCode == null) {
        this.setData({
          licenceCode: ''
        })
      } else {
        this.setData({
          licenceCode: data.licenceCode
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      state: options.state,
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      list_gao: wx.getMenuButtonBoundingClientRect().top + 50,
      xu_id: options.id
    })
    this.xuke_xq()
  },
  showModal(e) {
    var img = e.currentTarget.dataset.img
    console.log(img)
    this.setData({
      fdimg: img,
    })
    let {
      type
    } = e.currentTarget.dataset, va = '';
    console.log(type)
    if (type == 's2') {
      va = "s2";
    }
    this.setData({
      [va]: true
    })
  },
  confirmbtn(e) {
    console.log(e)
    console.log('点击了从新上传！')
    var aaa = this.data.fdaaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        // that.data.xkzlist[that.data.cxscindex].licenceImg = [urls];
        that.setData({
          licenceImg: urls,
          wulicenceImg: data.imgUrl
        })
      })
    })

  },
  cancelBtn() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.fdimg]
    })
  },
  // 返回
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})